/*
 * @lc app=leetcode.cn id=3 lang=javascript
 *
 * [3] 无重复字符的最长子串
 */

// @lc code=start
/**
 * @param {string} s
 * @return {number}
 */
var lengthOfLongestSubstring = function (s) {
  let hash = new Set();
  let rightK = 0;
  let ans = 0;
  for (let left = 0; left+ans < s.length; left++) {
    if (left != 0) {
      hash.delete(s[left - 1]);
    }
    while (rightK < s.length && !hash.has(s[rightK])) {
      hash.add(s[rightK]);
      rightK++;
    }
    ans = Math.max(ans, hash.size);
  }
  return ans
};//标准解法再加上之前的优化即可,最完美解法了
// @lc code=end
lengthOfLongestSubstring("abcdegfalcdegkop22");
var lengthOfLongestSubstring = function (s) {
  let set = new Set();
  let max = 0;
  let left = 0; //滑动窗口左边
  let right = 0; //滑动窗口右边
  for (; max < s.length - left; left++) {
    for (; right < s.length; right++) {
      if (!set.has(s[right])) {
        set.add(s[right]);
      } else {
        max = Math.max(set.size, max);
        set = new Set();
        right = left + 1;
        break;
      }
    }
  }
  return Math.max(set.size, max);
}; //这个属于暴力解法加了一点优化，真正的答案在上面
